#include "stdio.h"
#include "stdint.h"
#include "time.h"

#define N       7
uint32_t row[N];
uint32_t tick[N];
uint8_t  mat[N][N];
uint64_t count = 0;
uint64_t dcount = 0;

void display_m()
{
    for (int i = 0; i < N; i++)
    {
        for (int j = 0, k = 0x01; j < N; j++, k<<=1)
        {
            if ((row[i] & k) != 0) {
                putchar('1');
            } else {
                putchar('0');
            }
            putchar(' ');
        }
        printf("\n");
    }
}

void display_mat()
{
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            printf("%d ", mat[i][j]);
        }
        printf("\n");
    }
}

uint32_t calc_tick(uint32_t val) {
    uint32_t res = 0, k = 0x01;
    for (int i = 0; i < N; i++, k<<=1) {
        if (val & k) {
            res++;
        }
    }
    return res;
}

uint32_t find_max(uint32_t* data, int len) {
    uint32_t max = 0;
    while (len > 0)
    {
        if (*data > max) {
            max = *data;
        }
        data++;
        len--;
    }
    return max;
}

void process()
{
    count++;
    // display_m();
    // printf("============\n");
    // 转换到mat
    for (size_t i1 = 0; i1 < N; i1++)
    {
        for (size_t j1 = 0, k = 0x01; j1 < N; j1++, k <<= 1)
        {
            if ((row[i1] & k) != 0)
            {
                mat[i1][j1] = 1;
            }
            else
            {
                mat[i1][j1] = 0;
            }
        }
    }
    // display_mat();
    // printf("***********\n");
    // 计票
    int k = 0, t = 0;
    for (t = 0; t < N; t++) {
        tick[t] = 0;
        for (k = 0; k < N; k++) {
            tick[t] += mat[k][t];
        }
    }
    // if (count % 10000000U == 0) 
    // {
    //     static time_t last_t = 0;
    //     printf("[ %llu / %llu ]tick[N]:", dcount, count);
    //     for (size_t t = 0; t < N; t++)
    //     {
    //         printf("%u ", tick[t]);
    //     }
    //     time_t curr_t = time(NULL);
    //     // printf(", spend: %ld ", curr_t - last_t);
    //     last_t = curr_t;
    //     // printf("\n");
    // }
    uint32_t max_tick_count = 0;
    uint32_t max_tick = find_max(tick, N);
    for (k = 0; k < N; k++)
    {
        if (tick[k] == max_tick)
        {
            max_tick_count++;
        }
    }
    if (max_tick_count != 1)
    {
        dcount++;
        {
            static time_t last_t = 0;
            // printf("[ %llu / %llu ]tick[N]:", dcount, count);
            printf("tick[N]:", dcount, count);
            for (size_t t = 0; t < N; t++)
            {
                printf("%u ", tick[t]);
            }
            time_t curr_t = time(NULL);
            // printf(", spend: %ld ", curr_t - last_t);
            last_t = curr_t;
            // printf("\n");
        }
        printf(" 流局\n");
        display_mat();
        printf("***********\n");
    }
    else
    {
        // printf("\n");
    }
}

void func(int n)
{
    row[n] = 0x01;
    if (n == N - 1)
    {
        for(int m = 0; m < N; m++, row[n] <<= 1)
        {
            process();
        }
    } else {
        for(int m = 0; m < N; m++, row[n] <<= 1)
        {
            func(n + 1);
        }
    }
}

int main() {
    for (size_t i = 0; i < N; i++) {
        row[i] = 0x01;
        tick[i] = 0;
        for (size_t j = 0; j < N; j++) {
            mat[i][j] = 0;
        }
    }

    func(0);

    printf("count:%llu, dcount:%llu, ratio:%f\n", count, dcount, dcount / (long double)count);
    return 0;
}